//
//  HMStatusCell.swift
//  iOSWeibo11
//
//  Created by Apple on 16/7/22.
//  Copyright © 2016年 Apple. All rights reserved.
//

import UIKit
import SnapKit

/// 微博cell, 包含 原创View, 被转发View,底部按钮View
class HMStatusCell: UITableViewCell {
    
    /// bottomView的顶部约束
    var bottomViewTopCons: Constraint?
    
    /// 微博模型
    var status: HMStatus? {
        didSet {
            // 在cell拿到模型的时候,将模型赋值给originalView
            origialView.status = status
            
            // cell拿到模型将被转发微博模型传递给被转发的View
            retweetView.retweetStatus = status?.retweeted_status
            
            // 更新约束不能直接更改参照
            // 1.记录下这个约束
            // 2.先删除这个约束
            // 3.在添加约束
            
            // 删除之前的约束
            bottomViewTopCons?.uninstall()
            
            // 如果没有被转发的微博就隐藏 被转发微博View隐藏
            if status?.retweeted_status == nil {
                // 没有被转发微博
                retweetView.hidden = true
                
                // 更新bottomView的顶部约束,等于原创View的底部
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    bottomViewTopCons = make.top.equalTo(origialView.snp_bottom).constraint
                })
            } else {
                retweetView.hidden = false
                // 有被转发微博
                // 更新bottomView的顶部约束等于被转发view的底部
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
                })
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setupUI()
        
        // 异步绘制
        self.layer.drawsAsynchronously = true
        
        // 栅格化, 其实是在显示之前生成一张图片,直接使用这张图片来显示(不用GPU根据Cell的layer再去计算内容)当图片不用的时候回销毁掉.会产生离屏渲染
//        self.layer.shouldRasterize = true
//        // 栅格化图片的比例
//        self.layer.rasterizationScale = UIScreen.mainScreen().scale
    }
    
    /// 设置UI界面
    private func setupUI() {
        self.contentView.backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)

        // 添加控件
        self.contentView.addSubview(origialView)
        self.contentView.addSubview(retweetView)
        self.contentView.addSubview(bottomView)
        
        // 添加约束
        origialView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(self.contentView)
            make.top.equalTo(self.contentView).offset(8)
            
            // 模拟原创view不固定的高度
//            let height = CGFloat(arc4random_uniform(60)) + 44
//            make.height.equalTo(height)
        }
        
        // 被转发微博View
        retweetView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(self.contentView)
            make.top.equalTo(origialView.snp_bottom)
        }
        
        // 底部view
        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(self.contentView)
            
            // SnapKit constraint就是创建的约束
            // 记录后续需要修改的约束
            bottomViewTopCons = make.top.equalTo(retweetView.snp_bottom).constraint
            
            make.height.equalTo(CellBottomViewHeight)
        }
        
        // 让cell的高度自己适应,其实是根据conetntView的高度来
        // 需要约束好contentView
        self.contentView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(self)
        }
        
        // contentView的高度约束需要参照里面的内容来
        self.contentView.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(bottomView)
        }
    }

    // MARK: - 懒加载
    /// 原创view
    private lazy var origialView: HMStatusOriginalView = HMStatusOriginalView()
    
    /// 被转发微博View
    private lazy var retweetView: HMStatusRetweetView = HMStatusRetweetView()
    
    /// 底部view
    private lazy var bottomView: HMStatusBottomView = HMStatusBottomView()
}
